<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	if(!$_RQ['id'] && !$_SESSION['taskid']){
		exi('请先选择专题','error','/task/');
	}
	if($_RQ['id']){
		$taskid=$_SESSION['taskid']=$_RQ['id'];
	}else{
		$taskid=$_SESSION['taskid'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'taskid'=>$taskid);
    $fields='';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_seckill_task_room',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	//清除不存在会场的商品
	$goods=pdo_getall('jy_seckill_task_goods',$params,array('roomid','id'));
	foreach($goods as &$gv){
		$isroom=pdo_getcolumn('jy_seckill_task_room',array('id'=>$gv['roomid']),'id');
		if(!$isroom){
			pdo_delete('jy_seckill_task_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;	
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'会场管理'),
			array('op'=>'edit','title'=>'添加会场'),
		),
		'list'=>array('data'=>'list','tip'=>'点击编辑会场--可以添加商品','list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),	
			array('name'=>'enabled','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),	
		),
		'action'=>array(
			array('action'=>'goods','op'=>'room','title'=>'商品列表','type'=>'href'),
		),
		),
	);
	$return['status']=array('关闭','开启');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_seckill_task_room',array('id'=>$id));
		//$item['goods']=pdo_getall('jy_seckill_task_goods',array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$id,'taskid'=>$_SESSION['taskid']));
	}else{
		$item=array();
	}
    $return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'会场设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'会场管理'),
			array('op'=>'edit','title'=>'添加会场')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'tag','title'=>'标签','type'=>'string','help'=>'显示在首页橱窗头部，和商品详情页面'),
			//array('name'=>'oldshow','title'=>'过期抢购','type'=>'radio','options'=>'oldshow'),
			array('name'=>'diypage','title'=>'自定义页面','type'=>'radio','options'=>'diypage'),
			array('name'=>'enabled','title'=>'状态','type'=>'radio','options'=>'status'),
			/*array('type'=>'array','name'=>'goods','add_tip'=>'添加商品',
				'list'=>array(
					array('name'=>'timeid','title'=>'时间段','type'=>'string'),
					array('name'=>'goodsid','title'=>'商品id','type'=>'string'),
					array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
					array('name'=>'total','title'=>'库存','type'=>'string'),
					array('name'=>'maxbuy','title'=>'限购','type'=>'string'),
				),
				'details'=>array(
					array('name'=>'timeid','title'=>'选择时间段','type'=>'radio','options'=>'times','help'=>'这里只显示关联专题开放的时间点，如果显示空，说明专题没有设置时间点'),
					array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'get_good','choose_title'=>'选择商品'),
					array('name'=>'price','title'=>'秒杀价格','type'=>'string'),
					array('name'=>'total','title'=>'库存','type'=>'string'),
					array('name'=>'maxbuy','title'=>'限购','type'=>'string'),
					array('name'=>'commission1','title'=>'一级分销','type'=>'string'),
					array('name'=>'commission2','title'=>'二级分销','type'=>'string'),
				)
			),*/
			array('name'=>'page_title','title'=>'页面标题','type'=>'string'),
			array('name'=>'share_title','title'=>'分享标题','type'=>'string'),
			array('name'=>'share_icon','title'=>'分享图标','type'=>'image'),
			array('name'=>'share_desc','title'=>'分享描述','type'=>'text'),
		)
	);
	$times=pdo_getall('jy_seckill_task_time',array('taskid'=>$_SESSION['taskid'],'uniacid'=>$_SESSION['uniacid']),'time','time');
	foreach($times as $k=>&$tv){
		$return['times'][$k]=$k.'点';
	}	
	$return['diypage']=array('0'=>'系统默认','1'=>'整点秒杀');
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['oldshow']=array('0'=>'不允许','1'=>'允许');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//$goods=$postdata['goods'];
	//unset($postdata['goods']);
	//$data=array_fields(array('name'),$postdata);
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_seckill_task_room',$postdata,array('id'=>$id));
				$taskid=pdo_getcolumn('jy_seckill_task_room',array('id'=>$id),'taskid');
			}else{
			    $postdata['uniacid']=$_SESSION['uniacid'];
				$postdata['taskid']=$_SESSION['taskid'];
				$taskid=$_SESSION['taskid'];
				$postdata['createtime']=time();
				pdo_insert('jy_seckill_task_room',$postdata);
				$id=$roomid=pdo_insertid();
			}	
			/*pdo_delete('jy_seckill_task_goods',array('taskid'=>$_SESSION['taskid'],'roomid'=>$id,'uniacid'=>$_SESSION['uniacid']));
			foreach($goods as $v){
					$upgoods=array();
					$upgoods['taskid']=$_SESSION['taskid'];
					$upgoods['roomid']=$id;
					$upgoods['uniacid']=$_SESSION['uniacid'];
					$upgoods['goodsid']=$v['goodsid'];
					$upgoods['price']=$v['price'];
					$upgoods['total']=$v['total'];
					$upgoods['maxbuy']=$v['maxbuy'];
					$upgoods['commission1']=$v['commission1'];
					$upgoods['commission2']=$v['commission2'];
					$upgoods['commission3']=$v['commission3'];
					$upgoods['timeid']=$v['timeid'];
					unset($v['id']);
					pdo_insert('jy_seckill_task_goods',$upgoods);
					$active_id=pdo_insertid();
					model('jy_weishop/activity')->create_active($upgoods['goodsid'],$active_id,$this->plugin_name);
			}*/
			$this->model('model')->setTaskCache($taskid);
			exi('设置成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_seckill_task_room',array('id'=>$id));
		pdo_delete('jy_seckill_task_goods',array('roomid'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_seckill_task_room',array('id'=>$id));
				pdo_delete('jy_seckill_task_goods',array('roomid'=>$id));
			}
		}
	}
	$this->model('model')->setTaskCache($_SESSION['taskid']);
	exi('删除成功！');
}
?>